/*
 * Copyright (C) 2005-2015 Schlichtherle IT Services.
 * All rights reserved. Use is subject to license terms.
 */

package org.truelicense.swing;

import javax.swing.JButton;
import javax.swing.SwingUtilities;

import org.truelicense.api.ConsumerLicenseManager;
import org.truelicense.api.i18n.Message;
import org.truelicense.swing.util.ComponentEnabler;
import org.truelicense.swing.util.Enabler;
import static org.truelicense.ui.LicenseWizardMessage.uninstall_failure;
import static org.truelicense.ui.LicenseWizardMessage.uninstall_prompt;
import static org.truelicense.ui.LicenseWizardMessage.uninstall_success;
import static org.truelicense.ui.LicenseWizardMessage.uninstall_uninstall;
import org.truelicense.ui.LicenseWizardState;

/**
 * @author Christian Schlichtherle
 */
final class UninstallPanel extends LicenseWorkerPanel {

    private static final long serialVersionUID = 1L;

    private final ConsumerLicenseManager manager;

    UninstallPanel(final LicenseManagementWizard wizard) {
        super(wizard);
        final Enabler uninstallButtonProxy = new ComponentEnabler() {
            static final long serialVersionUID = 0L;

            @Override protected JButton component() { return uninstallButton; }
        };
        this.manager = new EnablingConsumerLicenseManager(wizard.nextButtonProxy(),
                new DisablingConsumerLicenseManager(uninstallButtonProxy,
                        wizard.manager()));
        initComponents();
    }

    @Override
    ConsumerLicenseManager manager() { return manager; }

    @Override
    public LicenseWizardState nextState() {
        return LicenseWizardState.uninstall;
    }

    @Override
    public void onAfterStateSwitch() {
        assert SwingUtilities.isEventDispatchThread();
        assert isVisible();
        wizard().disableNextButton();
        setStatusMessage(EMPTY_MESSAGE);
        final boolean installed = licenseInstalled();
        uninstallButton.setEnabled(installed);
        if (installed) uninstallButton.requestFocusInWindow();
    }

    @Override
    void setStatusMessage(Message message) {
        status.setText(message.toString());
    }

    @Override
    Message successMessage() {
        return uninstall_success.format(subject());
    }

    @Override
    Message failureMessage(Throwable throwable) {
        return uninstall_failure.format(subject());
    }

    /**
     * This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {
        java.awt.GridBagConstraints gridBagConstraints;

        javax.swing.JTextArea prompt = new javax.swing.JTextArea();
        uninstallButton = new org.truelicense.swing.util.EnhancedButton();
        status = new javax.swing.JTextArea();

        setName(UninstallPanel.class.getSimpleName());
        setLayout(new java.awt.GridBagLayout());

        prompt.setEditable(false);
        prompt.setFont(getFont());
        prompt.setLineWrap(true);
        prompt.setText(format(uninstall_prompt)); // NOI18N
        prompt.setWrapStyleWord(true);
        prompt.setBorder(null);
        prompt.setName(uninstall_prompt.name());
        prompt.setOpaque(false);
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 0;
        gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
        gridBagConstraints.ipady = 5;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.SOUTH;
        gridBagConstraints.weightx = 1.0;
        gridBagConstraints.insets = new java.awt.Insets(0, 0, 15, 0);
        add(prompt, gridBagConstraints);

        uninstallButton.setText(format(uninstall_uninstall)); // NOI18N
        uninstallButton.setName(uninstall_uninstall.name());
        uninstallButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                uninstallActionPerformed(evt);
            }
        });
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 1;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
        add(uninstallButton, gridBagConstraints);

        status.setEditable(false);
        status.setFont(getFont());
        status.setBorder(null);
        status.setName("status"); // NOI18N
        status.setOpaque(false);
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 2;
        gridBagConstraints.gridwidth = java.awt.GridBagConstraints.REMAINDER;
        gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
        gridBagConstraints.insets = new java.awt.Insets(15, 0, 0, 0);
        add(status, gridBagConstraints);
    }// </editor-fold>//GEN-END:initComponents

    private void uninstallActionPerformed(java.awt.event.ActionEvent ignored) {//GEN-FIRST:event_uninstallActionPerformed
        new LicenseWorker() {
            @Override protected Void doInBackground() throws Exception {
                manager().uninstall();
                return null;
            }
        }.execute();
    }//GEN-LAST:event_uninstallActionPerformed

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JTextArea status;
    private org.truelicense.swing.util.EnhancedButton uninstallButton;
    // End of variables declaration//GEN-END:variables
}
